
package com.catsic.zcjc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.zcjc.condition.MarkLabelCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.MarkLabelDao;
import com.catsic.zcjc.entity.MarkLabel;
import com.catsic.zcjc.exception.ZcjcExceptionCode;
import com.catsic.zcjc.form.MarkLabelForm;
import com.catsic.zcjc.service.MarkLabelService;
import com.catsic.zcjc.specifiction.MarkLabelSpec;


@Service("markLabelService")
public class MarkLabelServiceImpl extends BaseServiceImpl<MarkLabel, String> implements MarkLabelService{
	
	@Autowired
	private MarkLabelDao markLabelDao;

	@Override
	public MarkLabelForm add(MarkLabelForm form) {
		this.checkLabel(form);
		
		MarkLabel mark = new MarkLabel();

		mark.setType(form.getType());
		mark.setLabel(form.getLabel());
		mark.setCreateId(form.getCreateId());
		mark.setCreateName(form.getCreateName());
		
		return ZcjcConverts.convert(super.add(mark));
	}
	

	@Override
	public MarkLabelForm update(MarkLabelForm form) {
		this.checkLabel(form);
		
		MarkLabel mark = findById(form.getId());
		mark.setType(form.getType());
		mark.setLabel(form.getLabel());
		
		return ZcjcConverts.convert(super.add(mark));
	}
	
	public void checkLabel(MarkLabelForm form) {
		if(hasSameLabel(form)){
			throw new CatsicException(ZcjcExceptionCode.标签管理_内容重复);
		}
	}

	public boolean hasSameLabel(MarkLabelForm form) {
		MarkLabel label = null;
		
		if(form.getType()==ZcjcConstants.LABEL_TYPE_COMMON){
			label = markLabelDao.findByLabelAndType(form.getLabel(), form.getType());
		}else{
			label = markLabelDao.findByLabelAndUser(form.getLabel(), form.getType(),form.getCreateId());
		}
		
		if(form.getId()==null){
			if(label!=null){
				return true;
			}
		}else{
			if(label!=null&&!label.getId().equals(form.getId())){
				return true;
			}
		}
		
		return false;
	}

	@Override
	public MarkLabel findById(String id){
		MarkLabel mark = getById(id);
		if(null == mark){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.LABEL);
		}
		return mark;
	}

	@Override
	public MarkLabelForm findMarkLabelById(String id){
		MarkLabel mark = getById(id);
		return ZcjcConverts.convert(mark);
	}
	
	public Page<MarkLabelForm> findAll(MarkLabelCondition condition, Pageable pageable){
		MarkLabelSpec spec = new MarkLabelSpec(condition);
		Page<MarkLabel> page = markLabelDao.findAll(spec, pageable);
		List<MarkLabelForm> listForm = new ArrayList<MarkLabelForm>();
		for(MarkLabel equip :page.getContent()){
			listForm.add(ZcjcConverts.convert(equip));
	    }
	    Page<MarkLabelForm> page_ = new PageImpl<MarkLabelForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}


	@Override
	public void delete(String id){
		MarkLabel equip = findById(id);
	
		super.delete(equip);
	}


	@Override
	public List<MarkLabel> findByUser(String userId) {
		return markLabelDao.findByUser(userId);
	}

}
